﻿// //------------------------------------------------------------------------------
// // <copyright file="" company="Dascoba Development">
// //  Copyright © Dascoba Development 2010
// // </copyright>
// //------------------------------------------------------------------------------
using System;

namespace Dascoba.Umb.FileManager
{
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    using Dascoba.Umb.FileManager.Config;
    using Dascoba.Umb.FileManager.Support;

    using umbraco.BusinessLogic.Actions;
    using umbraco.cms.presentation.Trees;

    public class loadRecycleBin : BaseTree
    {
        #region Fields

        private readonly string userClientRoot;

        #endregion

        /// <summary>
        ///   Initializes a new instance of the <see cref = "loadRecycleBin" /> class.
        /// </summary>
        /// <param name = "application">The application.</param>
        public loadRecycleBin(string application)
            : base(application)
        {
        }

        /// <summary>
        ///   Creates the root node.
        /// </summary>
        /// <param name = "rootNode">The root node.</param>
        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            if (this.IsDialog)
            {
                rootNode.Source = null;
                return;
            }

            var recycleBinInfo = new DirectoryInfo(RecycleBinConfig.RecycleBinFolderPath);
            bool visibleFiles, visibleFolders;
            rootNode.HasChildren = FileManagerConfig.HasVisibleItems(
                recycleBinInfo, false, out visibleFiles, out visibleFolders);

            rootNode.Icon = rootNode.HasChildren ? Constants.BinTreeIcon : Constants.BinTreeIconClosed;
            rootNode.OpenIcon = Constants.BinTreeIcon;
            rootNode.NodeType = "initRecycleBin"; // this.TreeAlias;
            rootNode.NodeID = Util.MapUrl(RecycleBinConfig.RecycleBinFolderPath);
            if (!rootNode.HasChildren)
            {
                rootNode.Source = null;
            }

            rootNode.Action = "javascript:viewRecycleBin()";
        }

        /// <summary>
        ///   Creates the root node actions.
        /// </summary>
        /// <param name = "actions">The actions.</param>
        protected override void CreateRootNodeActions(ref List<umbraco.interfaces.IAction> actions)
        {
            actions.Clear();
            actions.AddRange(Menus.RecycleBinMenu);
            actions.Add(ContextMenuSeperator.Instance);
            actions.Add(ActionRefresh.Instance);
        }

        /// <summary>
        ///   Renders the specified tree.
        /// </summary>
        /// <param name = "tree">The tree.</param>
        public override void Render(ref XmlTree tree)
        {
            if (this.IsDialog)
            {
                return;
            }

            var recycledItems = RecycleBinConfig.Items;

            foreach (var recycledItem in recycledItems)
            {
                XmlTreeNode treeNode;
                switch (recycledItem.Type)
                {
                    case RecycledItemType.Folder:
                        {
                            treeNode = this.GetFolderNode(recycledItem);
                        }
                        break;
                    case RecycledItemType.File:
                        {
                            treeNode = this.GetFileNode(recycledItem);
                        }
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                if (treeNode != null)
                {
                    tree.Add(treeNode);
                }
            }
        }

        /// <summary>
        ///   Renders the JS.
        /// </summary>
        /// <param name = "javascript">The javascript.</param>
        public override void RenderJS(ref StringBuilder javascript)
        {
            if (this.IsDialog)
            {
                return;
            }

            // Recycled Item Viewer Function
            javascript.AppendFormat(
                @"
                // Recycled Actions //
                function viewRecycleBin() {{
                    parent.right.document.location.href = '{0}/Pages/RecycleBinViewer.aspx';}}

                function viewRecycledItem(folder) {{
                    parent.right.document.location.href = '{0}/Pages/RecycledItemViewer.aspx?folder=' + folder;}}
                ",
                Constants.FileManagerGlobalUrl);
        }

        /// <summary>
        ///   Gets the folder node.
        /// </summary>
        /// <param name = "recycledItem">The recycled item.</param>
        /// <returns></returns>
        private XmlTreeNode GetFolderNode(RecycledItem recycledItem)
        {
            var recycledItemNode = XmlTreeNode.Create(this);
            recycledItemNode.NodeID = recycledItem.Name;
            recycledItemNode.Icon = Constants.FolderTreeIcon;
            recycledItemNode.Text = new DirectoryInfo(Util.MapPath(recycledItem.RestorePath)).Name;

            recycledItemNode.NodeType = "initRecycledItem";
            recycledItemNode.Action = "javascript:viewRecycledItem()";

            // Build Context Menu
            recycledItemNode.Menu = Menus.RecycledItemMenu;

            return recycledItemNode;
        }

        /// <summary>
        ///   Gets the file node.
        /// </summary>
        /// <param name = "recycledItem">The recycled item.</param>
        /// <returns></returns>
        private XmlTreeNode GetFileNode(RecycledItem recycledItem)
        {
            var recycledItemNode = XmlTreeNode.Create(this);
            recycledItemNode.NodeID = recycledItem.Name;
            recycledItemNode.Icon = FileManagerConfig.GetFileIcon(Util.GetSimpleExtension(recycledItem.Name).ToLower());
            recycledItemNode.Text = Path.GetFileName(Util.MapPath(recycledItem.RestorePath));

            recycledItemNode.NodeType = "initRecycledItem";
            recycledItemNode.Action = "javascript:viewRecycledItem();";

            // Build Context Menu
            recycledItemNode.Menu = Menus.RecycledItemMenu;

            return recycledItemNode;
        }
    }
}